package com.schoolcloud.common.interceptors;

import cn.hutool.core.util.StrUtil;
import com.schoolcloud.common.util.UserContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * 如果请求头中有“user-id"，那么将其存入threadLocal
 *
 * @author hongmizfb
 * @date 2025/01/24
 */
public class UserInfoInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userId = request.getHeader("user-id");
        String userType = request.getHeader("user-type");
        boolean isAdmin =Boolean.parseBoolean(request.getHeader("is-admin"));
        if (StrUtil.isNotBlank(userId)) {
//            如果请求头中有用户信息，就存入线程变量中
            UserContext.setUser(userId);
            UserContext.setUserType(userType);
            UserContext.setIsAdmin(isAdmin);
        }
        return true;
    }
}
